package com.maven.front.controller.manageArticle;

import com.maven.front.bean.EduArticle;
import com.maven.front.bean.EduArticleContent;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduArticleDto;
import com.maven.front.service.manageArticle.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("admin/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    //列表显示
    @GetMapping("list")
    public Result list(){
        List<EduArticle> list=articleService.Managerlist();
        if(list==null){
            return  new Result(400,"没有数据",null);
        }
        else {
            return  new Result(200,"查询成功",list);
        }
    }
    //修改
    @PostMapping("update")
    public Result update(EduArticleDto eduArticleDto){
        eduArticleDto.setCreateTime(new Date());
        int res=articleService.update(eduArticleDto);
        if(res>0){
            return  new Result(200,"修改成功",1);
        }

        return  new Result(400,"修改失败",null);
    }

    //逻辑删除
    @PostMapping("delect")
    public Result delect(EduArticle eduArticle){
        int res=articleService.delect(eduArticle);
        if(res>0){
            return  new Result(200,"删除成功",1);
        }

        return  new Result(400,"删除失败",null);
    }

    //模糊查询
    @PostMapping("dimSelect")
    public Result dimSelect(EduArticleDto eduArticleDto){
        log.info(eduArticleDto.getTitle()+"*****************************"+eduArticleDto.getPublishTime()+"*******************"+eduArticleDto.getCreateTime());
        List<EduArticle> list =articleService.dimSelect(eduArticleDto);
        if(list==null){
            return new Result(400,"没有数据",null);
        }
        else {
            return  new Result(200,"查询成功",list);
        }
    }

    //编辑赋值
    @PostMapping("getById")
    public  Result getById(Integer id){
        EduArticle ea=articleService.getById(id);
        log.debug(ea.toString());
        if(ea==null){
            return new Result(400,"没有数据",null);
        }
        else {
            return  new Result(200,"查询成功",ea);
        }
    }

   //描述修改
   @PostMapping("updateSelect")
    public Result updateSlect(EduArticleContent eduArticleContent){
        int res=this.articleService.updateselect(eduArticleContent);
        if(res>0){
            return  new Result(200,"修改成功");
        }

        return  new Result(400,"修改失败");
    }

//描述赋值
@GetMapping("selectId")
    public Result selectId(int id){
        EduArticleContent eds=articleService.selectId(id);
        if(eds==null){
            return new Result(400,"没有数据",null);
        }
        else {
            return  new Result(200,"查询成功",eds);
        }
    }

    }

